package com.lucenly.card.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lucenly.card.R;
import com.lucenly.card.activity.MonthActivity;
import com.lucenly.card.activity.MyMoneyActivity;
import com.lucenly.card.activity.ProblemActivity;
import com.lucenly.card.activity.SettingActivity;
import com.lucenly.card.base.BaseFragment;
import com.lucenly.card.bean.ShareBean;
import com.lucenly.card.bean.UserInfo;
import com.lucenly.card.constants.Constants;
import com.lucenly.card.constants.UrlDefend;
import com.lucenly.card.event.StringMsgEvent;
import com.lucenly.card.utils.PublicUtil;
import com.lucenly.card.view.dialog.LianxiDialog;
import com.lucenly.card.view.dialog.ShareDialog;
import com.lucenly.card.view.refresh2.RefreshLayout;
import com.lucenly.card.view.refresh2.SmartRefreshLayout;
import com.lucenly.card.view.refresh2.listener.OnRefreshListener;
import com.siberiadante.toastutils.ToastUtil;
import com.vise.xsnow.dialog.PromptDialog;
import com.vise.xsnow.event.IEvent;
import com.vise.xsnow.event.Subscribe;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.utls.SharedPreferencesUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;

import butterknife.BindView;

public class AcountFragment extends BaseFragment{
    @BindView(R.id.ll_set)
    LinearLayout ll_set;
    LianxiDialog lianxiDialog;
    @BindView(R.id.ll_lianxi)
    LinearLayout ll_lianxi;
    @BindView(R.id.ll_money)
    LinearLayout ll_money;
    @BindView(R.id.iv_img)
    SimpleDraweeView iv_img;
    @BindView(R.id.tv_id)
    TextView tv_id;
    @BindView(R.id.tv_dkjl)
    TextView tv_dkjl;
    @BindView(R.id.tv_dkcs)
    TextView tv_dkcs;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.tv_status)
    TextView tv_status;
    @BindView(R.id.ll_problem)
    LinearLayout ll_problem;
    @BindView(R.id.ll_dk)
    LinearLayout ll_dk;
    @BindView(R.id.tv_qianbao)
    TextView tv_qianbao;
    @BindView(R.id.ll_yq)
    LinearLayout ll_yq;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.iv_xy)
    ImageView iv_xy;
    @BindView(R.id.ll_wx)
    LinearLayout ll_wx;
    String wxName = "";
    String wxUrl = "";
    ShareDialog shareDialog;
    @Override
    protected int getLayoutID() {
        return R.layout.fragment_account;
    }
    @Override
    protected void initView(View contentView) {

    }

    @Override
    protected void bindEvent() {
        C(ll_set);
        C(ll_lianxi);
        C(ll_money);
        C(ll_problem);
        C(ll_dk);
        C(ll_yq);
        C(iv_xy);
        C(ll_wx);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                initData();
            }
        });
    }

    @Override
    protected void initData() {
        UserInfo user = SharedPreferencesUtil.getInstance().getObject("user", UserInfo.class);
        if(user!=null){
            iv_img.setImageURI(user.getAvatar());
            tv_id.setText("我的ID: "+user.getId());
            tv_dkjl.setText(user.getRewardTotalFee()+"元");
            tv_dkcs.setText(user.getPunchDays()+"次");
            tv_time.setText(Constants.time);
            getData();
        }
        getKefu();
    }

    private void getKefu() {
        PublicUtil.getKefu(new ACallback<String>() {
            @Override
            public void onSuccess(String data) {
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    lianxiDialog = new LianxiDialog(getActivity(),jsonObject.getString("value"),jsonObject.getString("url"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                ToastUtil.showSingletonShort(errMsg);
            }
        });
    }

    @Override
    protected void lazyLoad() {
    }

    private void getData() {
        ViseHttp.GET(UrlDefend.GET_STATUS)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        refreshLayout.finishRefresh();
                        try {
                            JSONObject jsonObject = new JSONObject(data);
                            int status = jsonObject.getInt("status");
                            switch (status){//1待发放 2已发放 3未参与打卡
                                case 1:
                                    tv_status.setText("待发放");
                                    break;
                                case 2:
                                    tv_status.setText("已发放");
                                    break;
                                case 3:
                                    tv_status.setText("未参与打卡");
                                    break;
                                case 4:
                                    tv_status.setText("未到打卡时间");
                                    break;
                                case 5:
                                    tv_status.setText("记得打卡哟");
                                    break;
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        refreshLayout.finishRefresh();
                    }
                });
        ViseHttp.GET(UrlDefend.PURSE)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        refreshLayout.finishRefresh();
                        try {
                            JSONObject jsonObject = new JSONObject(data);
                           double balanceFee = jsonObject.getDouble("balanceFee");
                            double depositTotalFee = jsonObject.getDouble("depositTotalFee");
                            String withdrawFee = jsonObject.getString("withdrawFee");
                            DecimalFormat df = new DecimalFormat("0.00");
                            tv_qianbao.setText("¥ "+df.format(balanceFee+depositTotalFee)+"元");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        refreshLayout.finishRefresh();
                    }
                });
    }
    @Override
    public boolean isRegisterEvent() {
        return true;
    }
    @Subscribe
    public void refresh(IEvent iEvent){
        if(iEvent!=null && iEvent instanceof StringMsgEvent){
            if(((StringMsgEvent) iEvent).getMsg().equals("TIXIAN")){
                getData();
            }
        }
    }
    @Override
    protected void processClick(View view) {
        switch (view.getId()){
            case R.id.ll_set:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            case R.id.ll_lianxi:
                if(lianxiDialog==null){
                    getKefu();
                }else{
                    lianxiDialog.show();
                }
                break;
            case R.id.ll_money:
                startActivity(new Intent(getActivity(), MyMoneyActivity.class));
                break;
            case R.id.ll_problem:
                startActivity(new Intent(getActivity(), ProblemActivity.class));
                break;
            case R.id.ll_dk:
                startActivity(new Intent(getActivity(), MonthActivity.class));
                break;
            case R.id.ll_yq:
//                new ShareDialog(mContext, shareBean).show();
                if(shareDialog==null){
                    PublicUtil.getShareConfig(getActivity(), new PublicUtil.shareInterface() {
                        @Override
                        public void shareSuscess(ShareBean shareBean) {
                            shareDialog = new ShareDialog(getActivity(),shareBean);
                            shareDialog.show();
                        }
                    });
                }else{
                    shareDialog.show();
                }
                break;
            case R.id.iv_xy:
                PromptDialog dialog = new PromptDialog(getActivity());
                dialog.showInfo("正在努力开发中...");
                break;
            case R.id.ll_wx:
                // 得到剪贴板管理器
                ClipboardManager cmb = (ClipboardManager)getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                String content = getResources().getString(R.string.app_name);
//                String content = "小米花花";
                cmb.setText(content.trim());
                ToastUtil.showSingletonShort("已复制【"+content+"】到你的剪切板。\n请打开你的微信粘贴关注");
                break;
        }
    }
}
